//
//  CourseCollectionViewCell.m
//  Arts1
//
//  Created by lanou on 15/8/29.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "CourseCollectionViewCell.h"
#import "AllHeader.h"
#import <QuartzCore/QuartzCore.h>
#import <Accelerate/Accelerate.h>
#import "UIImage+ImageBlur.h"
#import <UIImageView+WebCache.h>

@implementation CourseCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH - 50)/4, (SCREEN_WIDTH - 50)/4)];
        _mainImageView.backgroundColor = kRandomColor;
        _mainImageView.layer.cornerRadius = 5;
        [_mainImageView setContentMode:(UIViewContentModeScaleAspectFill)];
        _mainImageView.layer.masksToBounds = YES;
//        _mainImageView.layer.opaque = YES;

        //  添加模糊效果
//        UIImage *image = [UIImage imageNamed:@"image005.jpg"];
//        [_mainImageView setContentMode:(UIViewContentModeScaleAspectFill)];
//       
//        _mainImageView.image = [image boxblurImageWithBlur:0.5];
//        [self.contentView addSubview:_mainImageView];
        

        _englishName = [[UILabel alloc] initWithFrame:CGRectMake(5, _mainImageView.height - 38, _mainImageView.width - 10, 20)];
//        _englishName.font = [UIFont boldSystemFontOfSize:11];
        _englishName.font = [UIFont fontWithName:@"AvenirNext-Bold" size:11];
        _englishName.textColor = [UIColor whiteColor];
//        _englishName.text = @"sketch";
        [_mainImageView addSubview:_englishName];
        
        _chinaName = [[UILabel alloc] initWithFrame:CGRectMake(5, _mainImageView.height - 20, _mainImageView.width - 10, 14)];
//        _chinaName.font = [UIFont systemFontOfSize:14];
        _chinaName.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        _chinaName.textColor = [UIColor whiteColor];
//        _chinaName.text = @"素描";
        [_mainImageView addSubview:_chinaName];
        
    }
    return self;
}

- (void)setCate:(CourseCateModel *)cate{
    _cate = cate;
    
    //  添加模糊效果
    
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:_cate.backImg] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority ];
    
    _mainImageView.image = [_mainImageView.image boxblurImageWithBlur:1.0];
    [self.contentView addSubview:_mainImageView];
    
    _chinaName.text = _cate.catName;
    _englishName.text = _cate.foreignName;

}

@end
//
//@implementation UIImage (Blur)
//
//-(UIImage *)boxblurImageWithBlur:(CGFloat)blur {
//    
//    NSData *imageData = UIImageJPEGRepresentation(self, 1); // convert to jpeg
//    UIImage* destImage = [UIImage imageWithData:imageData];
//    
//    
//    if (blur < 0.f || blur > 1.f) {
//        blur = 0.5f;
//    }
//    int boxSize = (int)(blur * 40);
//    boxSize = boxSize - (boxSize % 2) + 1;
//    
//    CGImageRef img = destImage.CGImage;
//    
//    vImage_Buffer inBuffer, outBuffer;
//    
//    vImage_Error error;
//    
//    void *pixelBuffer;
//    
//    
//    //create vImage_Buffer with data from CGImageRef
//    
//    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
//    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
//    
//    
//    inBuffer.width = CGImageGetWidth(img);
//    inBuffer.height = CGImageGetHeight(img);
//    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
//    
//    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
//    
//    //create vImage_Buffer for output
//    
//    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
//    
//    if(pixelBuffer == NULL)
//        NSLog(@"No pixelbuffer");
//    
//    outBuffer.data = pixelBuffer;
//    outBuffer.width = CGImageGetWidth(img);
//    outBuffer.height = CGImageGetHeight(img);
//    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
//    
//    // Create a third buffer for intermediate processing
//    void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
//    vImage_Buffer outBuffer2;
//    outBuffer2.data = pixelBuffer2;
//    outBuffer2.width = CGImageGetWidth(img);
//    outBuffer2.height = CGImageGetHeight(img);
//    outBuffer2.rowBytes = CGImageGetBytesPerRow(img);
//    
//    //perform convolution
//    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
//    if (error) {
//        NSLog(@"error from convolution %ld", error);
//    }
//    error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
//    if (error) {
//        NSLog(@"error from convolution %ld", error);
//    }
//    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
//    if (error) {
//        NSLog(@"error from convolution %ld", error);
//    }
//    
//    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//    CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
//                                             outBuffer.width,
//                                             outBuffer.height,
//                                             8,
//                                             outBuffer.rowBytes,
//                                             colorSpace,
//                                             (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
//    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
//    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
//    
//    //clean up
//    CGContextRelease(ctx);
//    CGColorSpaceRelease(colorSpace);
//    
//    free(pixelBuffer);
//    free(pixelBuffer2);
//    CFRelease(inBitmapData);
//    
//    CGImageRelease(imageRef);
//    
//    return returnImage;
//}

//@end
